<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
slice 3
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "fmt"

var x = []int{2: 5, 6, 0: 7}

func main() {
	fmt.Println(x)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">[7 0 5 6]</label></li>
<li><label for="choiceB">[7 6 5]</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: [7 0 5 6]
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/Sap3OfyWD7u">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
keyed elements (here they are <code class="tmd-code-span">2: 5</code> and <code class="tmd-code-span">0: 7</code>) and un-keyed elements (here it is <code class="tmd-code-span">6</code>) may be mixed up in array and slice composite literals.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
the index of an un-keyed element is determined by the index of the last keyed element. If all previous elements are un-keyed, then its index is its order number (zero based). So the index of the element <code class="tmd-code-span">6</code> is 3.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
the length of an array or slice composite literal is the largest element index plus one.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
unspecified elements are viewed as the zero value of the element type.
</div>
</li>
</ul>
<p></p>
<div class="tmd-usual">
The following is another example, which prints <code class="tmd-code-span">7 88</code>.
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
	var x = []int{4:44, 55, 66, 1:77, 88}
	println(len(x), x[2])
}
</code></pre>
<p></p>
</div>
<p></p>
</div>
